﻿using Aixiu.DAL;
using AiXiu.Common;
using AiXiu.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AiXiu.BLL
{
    public class LoginBLL
    {
        /// <summary>
        /// 通过用户名登录
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public OperResult<TBUsers> LoginByName(string userName, string password)
        {
            //a. 参数非空验证
            if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password))
            {
                return OperResult<TBUsers>.Failed("用户名或密码不能为空");
            }
            //b.通过用户名查询用户，不存在返回错误信息提示
            LoginDAL loginDAL = new LoginDAL();
            TBLogins logins = loginDAL.getUserByName(userName);
            if (logins == null)
            {
                return OperResult<TBUsers>.Failed("用户名不存在！");
            }
            //c. 验证用户密码，SHAEncryption类中方法
            SHAEncryption encryption = new SHAEncryption();
            if (encryption.SHA1Encrypt(password) != logins.Password)
            {
                return OperResult<TBUsers>.Failed("输入的密码有误！");
            }
            // 查找用户的具体信息
            UsersBLL usersBLL = new UsersBLL();
            TBUsers users = usersBLL.get(logins.Id);
            return OperResult<TBUsers>.Succeed(users);
        }
        /// <summary>
        /// 通过手机号登录
        /// </summary>
        /// <param name="userMobile"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public OperResult<TBUsers> getUserByMobile(string userMobile, string password)
        {
            if (string.IsNullOrEmpty(userMobile) || string.IsNullOrEmpty(password))
            {
                return OperResult<TBUsers>.Failed("手机号或密码不能为空");
            }
            LoginDAL loginDAL = new LoginDAL();
            TBLogins logins = loginDAL.getUserByMobile(userMobile);
            if (logins == null)
            {
                return OperResult<TBUsers>.Failed("手机号不存在！");
            }
            SHAEncryption encryption = new SHAEncryption();
            if (encryption.SHA1Encrypt(password) != logins.Password)
            {
                return OperResult<TBUsers>.Failed("输入的密码有误！");
            }
            UsersBLL usersBLL = new UsersBLL();
            TBUsers users = usersBLL.get(logins.Id);
            return OperResult<TBUsers>.Succeed(users);
        }
        /// <summary>
        /// 注册用户
        /// </summary>
        /// <param name="users">登录信息</param>
        /// <returns></returns>
        public OperResult Register(TBLogins login)
        {
            //1) 参数非空验证
            if (string.IsNullOrEmpty(login.UserName))
            {
                return OperResult.Failed("用户名不能为空");
            }
            if (string.IsNullOrEmpty(login.MobileNumber))
            {
                return OperResult.Failed("手机号不能为空");
            }
            if (string.IsNullOrEmpty(login.Password))
            {
                return OperResult.Failed("密码不能为空");
            }

            //2) 判断用户名是否已注册（UserDAL中调用方法）
            LoginDAL loginDAL = new LoginDAL();
            if (loginDAL.ExistUserName(login.UserName))
            {
                return OperResult.Failed("该用户名已注册");
            }
            //3) 判断手机号是否已注册（UserDAL中调用方法）
            if (loginDAL.ExistUserMoblie(login.MobileNumber))
            {
                return OperResult.Failed("该手机号已注册");
            }

            //4) 加密用户密码
            SHAEncryption encryption = new SHAEncryption();
            login.Password = encryption.SHA1Encrypt(login.Password);

            //5) 注册（UserDAL中调用方法）—— try catch
            try
            {
                loginDAL.Register(login);

            }
            catch (Exception ex)
            {
                LogHelper.Error(ex.ToString());
                return OperResult.Failed("注册失败：失败信息" + ex.ToString());
            }

            // 返回结果
            return OperResult.Succeed();
        }
    }
}
